package org.jiamandan.com.strategy.listener;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.strategy.IDXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.springframework.stereotype.Service;

@Service(value = DXConstant.Service.CMD_A04)
@AllArgsConstructor
@Slf4j
public class DXA04IssueChargeListenerService implements IDXContextListener {

   /* private IOrderChargeService iOrderChargeService;
    private NoticeManger noticeManger;
    private RocketMQTemplate rocketMQTemplate;
    private MQProperties mqProperties;*/


    @Override
    public void doExecute(DXBasicDto dto, Channel ctx) throws Exception {
        log.info("接收嘉满电-DX获取A04设备充电指令(CMD=A04)【"+dto.getDeviceNo()+"】报文:" + JSON.toJSONString(dto));
        String payload = dto.getData();
        String id = payload.substring(0, 8);//命令流水
        String orderNo = StringUtils.substring(payload, 8, 40);
        //根据流水号找->orderNo
        String isCharge = StringUtils.substring(payload, 40, 42);
        //86806507 38 780960
        //todo 结算结算订单

      /*  OrderCharge order = iOrderChargeService.getByOrderNo(CommonUtil.removeZero(orderNo,true));
        if(null!=order) {
            if (null != order.getPayType() && order.getPayType().equals(BizConstant.payType.MONTHLY_CARD_PAY)) {
            } else {
                if (isCharge.equals("01")) {
                   // 流水重复
                    iOrderChargeService.handleAbnormalCharge(Func.toStr(order.getId()), "重复下发充电");
                }
                if (isCharge.equals("02")) {
                    // 通道被占用不能充电
                    iOrderChargeService.handleAbnormalCharge(Func.toStr(order.getId()), "通道被占用不能充电");
                    noticeManger.noticeOrderFail(order,"通道被占用不能充电");
                }
                if (isCharge.equals("03")) {
                    // 通道故障不能充电
                    iOrderChargeService.handleAbnormalCharge(Func.toStr(order.getId()), "通道故障不能充电");
                }
                if (isCharge.equals("04")) {
                    // 参数错误
                    iOrderChargeService.handleAbnormalCharge(Func.toStr(order.getId()), "参数错误");
                }
                //充电成功修改订单状态
                if(isCharge.equals("00")){
                    order.setOrderStatus(BizConstant.OrderStatus.CHARGING);
                    iOrderChargeService.updateById(order);
                    noticeManger.noticeOrderSuccess(order);
                    log.info("嘉满电-接受DX(CMD=A04)【命令，充电设备={},订单号={}",dto.getDeviceNo(),orderNo);
                    //rocketMQTemplate.asyncSend(mqProperties.getOrderChargeProfitTopic() + ":" + BizEventTag.SUB_MSG.name(), order, RocketmqSendCallbackBuilder.commonCallback());
                    Message<String> messagePayload = MessageBuilder.withPayload(order.getOrderNo()).build();
                    rocketMQTemplate.asyncSend(mqProperties.getOrderChargeProfitTopic() + ":" + BizEventTag.SUB_MSG.name(),
                        messagePayload, RocketmqSendCallbackBuilder.commonCallback());

                    //推送给支付宝信息=>充电中
                    rocketMQTemplate.asyncSend(mqProperties.getOrderChargeProfitTopic() + ":" + BizEventTag.ORDER_CHARGE_STATUS_REPORT_ALI.name(),
                        order, RocketmqSendCallbackBuilder.commonCallback());
                }
            }
        }
*/
    }
}

